package TagRate.project.client;

import TagRate.project.pages.client.Base;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.web_bloks.api.client.User;
import com.web_bloks.content.client.ContentPage;
import com.web_bloks.system.client.Logger;
import com.web_bloks.url.client.UrlFields;

/**
 * 
 * @author Jean-Lou Dupont
 * @deprecated
 */
public class ContentManager {

	static Base base = null;
	static PageManager pm = null;
	
	public ContentManager() {	
	}
	/*****************************************
	 * CONFIGURATION
	 *****************************************/
	public void setBase( Base b ) {
		base = b;
	}
	public Base getBase() {
		return base;
	}
	public void setPageManager(PageManager p) {
		pm = p;
	}
	/*****************************************
	 * API
	 *****************************************/
	public boolean displayPage(UrlFields uf) {
		return displayPage( uf.page );
	}
	
	public boolean displayPage(String pageName) {
	
		ContentPage p = pm.getContentPage(pageName);
		
		if (p == null)
			return false;
		
		Logger.logInfo(this.getClass()+".displayPage: name["+p.getPageName()+"]");
		
		if (p.requiresLogin()) {
			UserManager um = GWT.create(UserManager.class);
			User cUser = um.getCurrentUser();

			if ( null==cUser || !cUser.isLogged()) {
				displayLoginNotice();
				navigateToMain();
				return false;
			}
		}
		
		base.setBody(p.getDisplayWidget());
		setWindowTitle(p.getPageName());
		
		return true;
	}
	
	protected void navigateToMain() {
		
		DeferredCommand.addCommand( new Command() {
			public void execute() {
				ContentPage p = pm.getContentPage("Main");
				base.setBody(p.getDisplayWidget());
				setWindowTitle(p.getPageName());
			}
		});
		
	}
	
	public void displayLoginNotice() {
		
		AppMessages MSG = GWT.create(AppMessages.class);
		MessageManager mm = GWT.create(MessageManager.class);
		
		mm.setMessage( MSG.login_notice() );
	}
	
	protected void setWindowTitle(String title ) {
		
		AppMessages MSG = GWT.create(AppMessages.class);
		
		Window.setTitle(MSG.site() + " - " + title );		
	}
}//END